<template>
  <div class="table-container">
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
          添加
        </el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          删除
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel>
        <el-form
          ref="form"
          :model="queryForm"
          :inline="true"
          @submit.native.prevent
        >
          <el-form-item>
            <el-input v-model="pagination.query" placeholder="查询" />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-search"
              type="primary"
              native-type="submit"
              @click="handleQuery"
            >
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :height="height"
      @selection-change="setSelectRows"
    >
      <el-table-column
        show-overflow-tooltip
        type="selection"
        width="55"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="序号" width="95">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="name"
        label="名称"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="分类"
        prop="category.name"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="图片">
        <template #default="{ row }">
          <el-image v-if="imgShow" :src="row.image.url"></el-image>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="状态"
        prop="state.name"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="目"
        prop="order.name"
      ></el-table-column>
      <el-table-column label="分布地区" width="300px">
        <template #default="{ row }">
          <el-tag v-for="i in row.address" :key="i.id">
            {{ i.name }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="时间"
        prop="datetime"
        width="200"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="180px">
        <template #default="{ row }">
          <el-button type="primary" @click="handleEdit(row)">编辑</el-button>
          <el-button type="primary" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="pagination.page"
      layout="total, sizes, prev, pager, next, jumper"
      :page-size="pagination.pageSize"
      :total="pagination.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <edit-dialog ref="edit" @fetchData="fetchData"></edit-dialog>
  </div>
</template>

<script>
  /*
  公用一个弹窗，向后台提交数据，如果有id，就是修改，没有id就是添加
   */

  import { getAllBird, deleteBird } from '@/api/bird'
  import EditDialog from '@/views/bird/show/components/EditDialog'

  export default {
    name: 'Show',
    components: {
      EditDialog,
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger',
        }
        return statusMap[status]
      },
    },
    data() {
      return {
        imgShow: true,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        background: true,
        selectRows: [],
        elementLoadingText: '正在加载...',
        pagination: {
          page: 1,
          pageSize: 10,
          total: 0,
          query: '',
        },

        queryForm: {
          selectQuery: '',
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    beforeDestroy() {},
    mounted() {},
    methods: {
      handleAdd() {
        this.$refs['edit'].showEdit()
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await deleteBird({ id: [row.id] })
            this.$baseMessage(msg, 'success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = []
            for (let i = 0; i < this.selectRows.length; i++) {
              ids.push(this.selectRows[i].id)
            }
            // this.selectRows.list((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await deleteBird({ id: ids })
              this.$baseMessage(msg, 'success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.pagination.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pagination.page = val
        this.fetchData()
      },
      //查询信息函数
      handleQuery() {
        this.pagination.page = 1
        this.fetchData()
      },
      //加载表格页面
      async fetchData() {
        this.listLoading = true
        const result = await getAllBird(this.pagination)
        this.list = result.data.birdVo
        this.pagination.total = result.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
      setSelectRows(val) {
        this.selectRows = val
        console.log(this.selectRows)
      },
    },
  }
</script>
